package com.xjtu.project.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjtu.project.common.utls.JsonResponse;
import com.xjtu.project.model.dto.OrderSubmitDTO;
import com.xjtu.project.model.dto.OrdersPageQueryDTO;
import com.xjtu.project.model.dto.PageDTO;
import com.xjtu.project.model.vo.OrderSubmitVO;
import com.xjtu.project.model.vo.OrdersVO;
import com.xjtu.project.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user/order")
public class UserOrdersController {
    @Autowired
    private OrdersService ordersService;
    @GetMapping("/historyOrders")
    public JsonResponse<PageDTO<OrdersVO>> getOrdersByPage(OrdersPageQueryDTO ordersPageQuery){
        return JsonResponse.success(ordersService.getMyOrders(ordersPageQuery));
    }
    @PutMapping("/cancel/{id}")
    public JsonResponse<String> cancel(@PathVariable Integer id){
        ordersService.cancelById(id);
        return JsonResponse.success(null);
    }
    @PostMapping("/submit")
    public JsonResponse<OrderSubmitVO> submit(@RequestBody OrderSubmitDTO submitDTO){
        return JsonResponse.success(ordersService.submit(submitDTO));
    }
    @GetMapping("/payment")
    public JsonResponse<String> payment(@RequestParam("orderNumber") String orderNumber){
        //根据订单号查询订单然后直接修改订单状态并且扣减用户余额
        ordersService.pay(orderNumber);
        return JsonResponse.success(null);
    }
}
